package com.heiyu.controller;

import com.google.common.collect.Maps;
import com.heiyu.controller.request.BannerUpdateRequest;
import com.heiyu.controller.request.BaseRequest;
import com.heiyu.controller.response.BaseResponse;
import com.heiyu.dao.BannerInfoMapper;
import com.heiyu.entity.BannerInfo;
import com.heiyu.utils.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Api
@RestController
@RequestMapping("banner")
public class BannerController extends AdminBaseController {


    @Autowired
    private BannerInfoMapper bannerInfoMapper;

    @ApiOperation(value = "list",notes = "banner列表")
    @RequestMapping(value = "list",method = RequestMethod.GET)
    public BaseResponse list(@ModelAttribute BaseRequest request){
        BaseResponse response = new BaseResponse();
        Map<String,Object> param = Maps.newHashMap();
        if(!StringUtils.isEmpty(request.getSingleKey())){
            param.put("position",request.getSingleKey());
        }
        param = PageHelper.page(bannerInfoMapper,param,request.getPageIndex(),request.getPageSize());
        response.success();
        response.setData(param);
        return response;
    }

    @ApiOperation(value = "get",notes = "获取banner详情")
    @RequestMapping(value = "get",method = RequestMethod.GET)
    public BaseResponse get(@ModelAttribute BaseRequest request){
        BaseResponse response = new BaseResponse();
        BannerInfo articleInfo = this.bannerInfoMapper.selectPrimary(Long.valueOf(request.getTargetId()));
        if(articleInfo == null){
            response.error("信息不存在");
        }else{
            response.success();
            response.setData(articleInfo);
        }
        return response;
    }

    @ApiOperation(value = "update",notes = "更新banner")
    @RequestMapping(value = "update",method = RequestMethod.POST)
    public BaseResponse update(@ModelAttribute BannerUpdateRequest bannerInfo){
        BaseResponse response = new BaseResponse();
        BannerInfo info = this.bannerInfoMapper.selectPrimary(bannerInfo.getId());
        if(info == null){
            response.error("信息不存在");
        }else{
            info.setImgUrl(bannerInfo.getImgUrl());
            info.setPosition(bannerInfo.getPosition());
            info.setTargetUrl(bannerInfo.getTargetUrl());
            info.setTitle(bannerInfo.getTitle());
            this.bannerInfoMapper.updateById(info);
            response.success();
        }
        return response;
    }

    @ApiOperation(value = "put",notes = "新增banner")
    @RequestMapping(value = "put",method = RequestMethod.POST)
    public BaseResponse put(@ModelAttribute BannerInfo bannerInfo){
        BaseResponse response = new BaseResponse();
        bannerInfo.setCreatedTime(new Date());
        bannerInfo.setModifyTime(new Date());
        bannerInfo.setStatus("1");
        bannerInfo.setIsDeleted("0");
        this.bannerInfoMapper.insert(bannerInfo);
        response.success();
        return response;
    }

    @ApiOperation(value = "del",notes = "删除banner")
    @RequestMapping(value = "del",method = RequestMethod.POST)
    public BaseResponse del(@ModelAttribute BaseRequest request){
        BaseResponse response = new BaseResponse();
        BannerInfo info = this.bannerInfoMapper.selectPrimary(Long.valueOf(request.getTargetId()));
        if(info == null){
            response.error("信息不存在");
        }else{
            this.bannerInfoMapper.delete(info.getId());
            response.success();
        }
        return response;
    }

    @ApiOperation(value = "top/update",notes = "置顶")
    @PostMapping(value = "top/update")
    public BaseResponse top(@ModelAttribute BaseRequest request){
        BaseResponse response = new BaseResponse();
        BannerInfo info = this.bannerInfoMapper.selectPrimary(Long.valueOf(request.getTargetId()));
        if(info == null){
            response.error("信息不存在");
        }else{
            BannerInfo bannerInfo = new BannerInfo();
            bannerInfo.setPosition(info.getPosition());
            List<BannerInfo> list = this.bannerInfoMapper.selectList(bannerInfo);
            if(list.get(0).getId()!=info.getId()){
                //交换两个的数据
                bannerInfo = list.get(0);
                info.setId(list.get(0).getId());
                bannerInfo.setId(Long.valueOf(request.getTargetId()));
                this.bannerInfoMapper.updateById(info);
                this.bannerInfoMapper.updateById(bannerInfo);
            }
            response.success();
        }
        return response;
    }

}
